package com.usian.goods.controller;

import com.usian.goods.common.R;
import com.usian.goods.config.AliyunOSSUtils;
import com.usian.goods.entity.ResultResponse;
import com.usian.goods.service.GoodsService;
import com.usian.goods.service.ITypeService;
import com.usian.model.goods.Goods;
import com.usian.goods.vo.GoodsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author wyp
 * @since 2024-12-17
 */
@RestController
@RequestMapping("/good")
@Transactional
@Slf4j
public class GoodsController {
    private final GoodsService goodsService;
    @Autowired
    private AliyunOSSUtils aliyunOSSUtils;

    @Autowired
    private ITypeService typeService;

    public GoodsController(GoodsService goodsService) {
        this.goodsService = goodsService;
    }

    @RequestMapping("/findList")
    public R findList() {
        List<Goods> list = goodsService.findGoods();
        return R.success(list);
    }


    @RequestMapping("/findGoods")
    public ResultResponse findGoods() {
        List<Goods> list = goodsService.list();
        //展示商品状态是上架
        list.removeIf(goods -> goods.getStatus() != 1);//移除状态不是上架的商品
        list.sort((o1, o2) -> o2.getId().compareTo(o1.getId()));//倒序
        return ResultResponse.ok().data("goodsList", list);
    }

    @RequestMapping("/findGoodsById")
    public ResultResponse findGoodsById(Integer id) {
        return ResultResponse.ok().data("goods", goodsService.getById(id));
    }

    @RequestMapping("/updateNumbers")
    public GoodsVo findScoreId(@RequestBody GoodsVo goodsVo) {
        return goodsService.findScoreId(goodsVo);
    }

    @RequestMapping("/shangGoods")
    public R shangGoods(Integer id) {
        goodsService.shangGoods(id);
        return R.success("商品上架成功");
    }

    @RequestMapping("/xiaGoods")
    public R xiaGoods(Integer id) {
        goodsService.xiaGoods(id);
        return R.success("商品下架成功");
    }

    @RequestMapping("/addGoods")
    public R addGoods(@RequestBody Goods goods) {
        goods.setNums(0);
        goods.setStatus(3);
        goods.setIsDelete(1);
        goods.setGoodsId(String.valueOf(System.currentTimeMillis()));
        goodsService.save(goods);
        return R.success(goods);
    }

    @RequestMapping("/updateGoods")
    public R updateGoods(@RequestBody Goods goods) {
        goodsService.updateById(goods);
        return R.success(goods);
    }

    @RequestMapping("/fileUpload")
    public String upload(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        InputStream inputStream = file.getInputStream();
        System.out.println(inputStream + "===========");
        String s = aliyunOSSUtils.putObject(inputStream, originalFilename);
        return s;
    }

    @RequestMapping("findType")
    public ResultResponse findType(){
        return ResultResponse.ok().data("typeList",typeService.list());
    }
    @RequestMapping("findGoodsByType")
    public ResultResponse findGoodsByType(Integer tid){
        return goodsService.findGoodsByType(tid);
    }



}
